﻿using IServices;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NDLS.WebApi.Common.JWT;
using NDLS.WebApi.Model;
using NDLS.WebApi.Model.ModelVM;

namespace NDLS.WebApi.Controllers
{
    /// <summary>
    /// 用户相关操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(AuthenticationSchemes = "Bearer")]
    public class UserController : Controller
    {
        private readonly IUserService _userService;
        private readonly ICaptcha _captcha;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="captcha"></param>
        public UserController(IUserService userService, ICaptcha captcha)
        {
            _userService = userService;
            _captcha = captcha;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public MsgRes Register(string username, string password)
        {
            return _userService.Register(username, password);
        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Captcha()
        {
            string id = "whx1004";
            var info = _captcha.Generate(id);
            var stream = new MemoryStream(info.Bytes);
            return File(stream, "image/gif");
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Policy = "AdminOrUser")]
        public SmPerson GetUserInfo()
        {
            return _userService.GetUserInfo();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]    
        public MsgRes Login(string username, string password, string code)
        {
            return _userService.Login(username, password, code);
        }
    }
}
